{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Duplicates from Sorted Array II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeDuplicates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除有序数组中的重复项 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个有序数组 <code>nums</code> ，请你<strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"> 原地</a></strong> 删除重复出现的元素，使得出现次数超过两次的元素<strong>只出现两次</strong> ，返回删除后数组的新长度。</p>\n",
    "\n",
    "<p>不要使用额外的数组空间，你必须在 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>说明：</strong></p>\n",
    "\n",
    "<p>为什么返回数值是整数，但输出的答案是数组呢？</p>\n",
    "\n",
    "<p>请注意，输入数组是以<strong>「引用」</strong>方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。</p>\n",
    "\n",
    "<p>你可以想象内部操作如下:</p>\n",
    "\n",
    "<pre>\n",
    "// <strong>nums</strong> 是以“引用”方式传递的。也就是说，不对实参做任何拷贝\n",
    "int len = removeDuplicates(nums);\n",
    "\n",
    "// 在函数里修改输入数组对于调用者是可见的。\n",
    "// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\n",
    "for (int i = 0; i &lt; len; i++) {\n",
    "&nbsp; &nbsp; print(nums[i]);\n",
    "}\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,1,1,2,2,3]\n",
    "<strong>输出：</strong>5, nums = [1,1,2,2,3]\n",
    "<strong>解释：</strong>函数应返回新长度 length = <strong><code>5</code></strong>, 并且原数组的前五个元素被修改为 <strong><code>1, 1, 2, 2, 3</code></strong>。 不需要考虑数组中超出新长度后面的元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,0,1,1,1,1,2,3,3]\n",
    "<strong>输出：</strong>7, nums = [0,0,1,1,2,3,3]\n",
    "<strong>解释：</strong>函数应返回新长度 length = <strong><code>7</code></strong>, 并且原数组的前五个元素被修改为&nbsp;<strong><code>0, 0, 1, 1, 2, 3, 3</code></strong>。不需要考虑数组中超出新长度后面的元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>nums</code> 已按升序排列</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-duplicates-from-sorted-array-ii](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-duplicates-from-sorted-array-ii](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,1,1,2,2,3]', '[0,0,1,1,1,1,2,3,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i = 2\n",
    "        for n in nums[2:]:\n",
    "            if n != nums[i-2]:\n",
    "                nums[i] = n\n",
    "                i += 1\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "#         i = 0\n",
    "#         for e in nums:\n",
    "#             if i < 2 or e != nums[i - 2]:\n",
    "#                 nums[i] = e\n",
    "#                 i += 1\n",
    "#             else:\n",
    "#                 continue\n",
    "#         return i\n",
    "        \n",
    "        i = 0\n",
    "        j = 2\n",
    "        while j < len(nums):\n",
    "            if nums[i] == nums[j]:\n",
    "                del nums[j]\n",
    "            else:\n",
    "                i += 1\n",
    "                j += 1\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        lastNum = None\n",
    "        appearance = 0\n",
    "        j = 0\n",
    "        for num in nums:\n",
    "            if num != lastNum:\n",
    "                lastNum = num\n",
    "                appearance = 1\n",
    "                nums[j] = lastNum\n",
    "                j += 1\n",
    "            elif appearance == 1:\n",
    "                appearance += 1\n",
    "                nums[j] = lastNum\n",
    "                j += 1\n",
    "        return j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        length = len(nums)\n",
    "        if length <= 2:\n",
    "            return length\n",
    "        sorted(nums)\n",
    "        print(nums)\n",
    "        i = 0\n",
    "        j = 0\n",
    "        last_num = None\n",
    "        last_num_count = 0\n",
    "        while j < length:\n",
    "            if nums[j] != last_num or (nums[j] == last_num and last_num_count <= 1):\n",
    "                last_num_count = 1 if nums[j] != last_num else last_num_count + 1\n",
    "                last_num = nums[j]\n",
    "                print(nums[j])\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "                j += 1\n",
    "            else:\n",
    "                last_num = nums[j]\n",
    "                last_num_count += 1\n",
    "                j += 1\n",
    "        print(nums)\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            j = i+1\n",
    "            flag = True\n",
    "            while j < len(nums):          \n",
    "                if nums[i]==nums[j]:\n",
    "                    if flag == True:\n",
    "                        j+=1\n",
    "                        flag = False\n",
    "                    else:\n",
    "                        nums.pop(j)\n",
    "                else:\n",
    "                    break\n",
    "            i=j\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        tag = []\n",
    "        if not nums:return 0\n",
    "        for j in range(1,len(nums)):\n",
    "            if j>1 and nums[j]==nums[j-1] and nums[j]==nums[j-2]:\n",
    "                tag.append(j)\n",
    "        for j in tag[::-1]:\n",
    "            nums.remove(nums[j])\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "#         if not nums:return\n",
    "#         newTail = 0\n",
    "#         cishu = 0\n",
    "#         for num in nums[1:]:\n",
    "#             if num == nums[newTail]:\n",
    "#                 cishu += 1\n",
    "#                 if cishu==1:\n",
    "#                     newTail+=1\n",
    "#                     nums[newTail] = num\n",
    "                    \n",
    "                    \n",
    "#                 else:\n",
    "#                     cishu = 1\n",
    "                    \n",
    "                        \n",
    "#             if num != nums[newTail] :\n",
    "#                 newTail+=1\n",
    "#                 nums[newTail] = num\n",
    "#                 cishu = 0\n",
    "#         return newTail+1\n",
    "\n",
    "        i = 0\n",
    "        for num in nums:\n",
    "            if i < 2 or num > nums[i-2]:\n",
    "                nums[i] = num\n",
    "                i += 1\n",
    "        return i  \n",
    "    \n",
    "   \n",
    "    \n",
    "    \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        j = 0\n",
    "        a = len(nums)\n",
    "        c = 0\n",
    "        i = 1\n",
    "        if a > 1:\n",
    "            while i < a:\n",
    "                    if nums[i]!=nums[j]:\n",
    "                            j = j+1\n",
    "                            nums[j] = nums[i]\n",
    "                            c = 0\n",
    "                    else:\n",
    "                        if c < 1:\n",
    "                            j = j+1\n",
    "                            nums[j] = nums[i]\n",
    "                            c = c + 1\n",
    "                    i = i+1\n",
    "            return j+1\n",
    "        else:\n",
    "            return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if not nums:\n",
    "            return 0\n",
    "        \n",
    "        last, i, same = 0, 1, False\n",
    "        while i < len(nums):\n",
    "            if nums[last] != nums[i] or not same:\n",
    "                same = nums[last] == nums[i]\n",
    "                last += 1\n",
    "                nums[last] = nums[i]\n",
    "            i += 1\n",
    "        return last + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n1,n2 = 0,0\n",
    "        aa = \"\"\n",
    "        while True:\n",
    "            try:\n",
    "                nums[n1]\n",
    "            except IndexError:\n",
    "                break\n",
    "            if nums[n1] != aa:\n",
    "                aa = nums[n1]\n",
    "                n2 = 1\n",
    "                n1 += 1\n",
    "            else:\n",
    "                if n2 == 2:\n",
    "                    nums.pop(n1)\n",
    "                else:\n",
    "                    n2 += 1\n",
    "                    n1 += 1\n",
    "        return(len(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums) < 3:\n",
    "            return len(nums)\n",
    "        position=left=0\n",
    "        right = 2\n",
    "        while right < len(nums):\n",
    "            if nums[left]==nums[right]:\n",
    "                left+=1\n",
    "                right+=1\n",
    "            else:\n",
    "                nums[position] = nums[left]\n",
    "                position+=1\n",
    "                left+=1\n",
    "                right+=1\n",
    "        # 最后两个元素是有效的\n",
    "        nums[position] = nums[left]\n",
    "        position+=1\n",
    "        nums[position] = nums[left+1]\n",
    "        return position+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return None\n",
    "        i, sub_length = 1, 1\n",
    "        while i < len(nums):\n",
    "            if nums[i] == nums[i - 1]:\n",
    "                if sub_length == 2:\n",
    "                    del nums[i]\n",
    "                else:\n",
    "                    sub_length += 1\n",
    "                    i += 1\n",
    "            else:\n",
    "                sub_length = 1\n",
    "                i += 1\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        # 暴力解法,hash表\n",
    "        # 双指针解法，单一元素\n",
    "        p = 0\n",
    "        q = 1\n",
    "        n = len(nums)\n",
    "        if not n :\n",
    "            return 0\n",
    "        \n",
    "        while q < len(nums):\n",
    "            # print(p,q)\n",
    "            if nums[p] == nums[q] and (q - p < 2):\n",
    "                q += 1\n",
    "            elif nums[p] == nums[q] and (q - p >= 2):\n",
    "                nums.pop(q)\n",
    "            elif nums[p] < nums[q]:\n",
    "                p = q\n",
    "                q += 1\n",
    "        # print(nums)\n",
    "        return len(nums) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        i = -1\n",
    "        j = 0\n",
    "        if n < 2:\n",
    "            return n\n",
    "        while j < n:\n",
    "            if j < 2:\n",
    "                i += 1\n",
    "                nums[i] = nums[j]\n",
    "                j += 1\n",
    "            else:\n",
    "                if nums[j] == nums[i] and nums[j] == nums[i-1]:\n",
    "                    j += 1\n",
    "                else:\n",
    "                    i += 1\n",
    "                    nums[i] = nums[j]\n",
    "                    j += 1\n",
    "        return i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 0:\n",
    "            return 0\n",
    "\n",
    "        count = 1\n",
    "        i = 1\n",
    "\n",
    "        for j in range(1, n):\n",
    "            if nums[j] == nums[j - 1]:\n",
    "                count += 1\n",
    "\n",
    "                if count > 2:\n",
    "                    # igore the triple num\n",
    "                    continue\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "            else:\n",
    "                count = 1\n",
    "                nums[i] = nums[j]\n",
    "                i += 1\n",
    "        \n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i = 0\n",
    "        for num in nums:\n",
    "            if i < 2 or num != nums[i - 2]:\n",
    "                nums[i] = num\n",
    "                i += 1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        i, j, n = 1, 2, len(nums)\n",
    "        while j < n:\n",
    "            if nums[j] == nums[i] and nums[j] == nums[i - 1]:\n",
    "                pass\n",
    "            else:\n",
    "                nums[i + 1] = nums[j]\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        l = len(nums)\n",
    "        if l <= 2:\n",
    "            return l\n",
    "        counter = 2\n",
    "        for i in range(2, l):\n",
    "            print(counter)\n",
    "            if nums[i] != nums[counter - 2]:\n",
    "                \n",
    "                nums[counter] = nums[i]\n",
    "                counter += 1\n",
    "        return counter\n",
    "        \n",
    "\n",
    "            \n",
    "\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if len(nums)<3:\n",
    "            return len(nums)\n",
    "        readp = 1\n",
    "        writep = 1\n",
    "        prev = nums[0]\n",
    "        two = False\n",
    "        while readp<len(nums):\n",
    "            if two==False and nums[readp]==prev:\n",
    "                print(readp,writep,prev)\n",
    "                two = True\n",
    "                nums[writep] = nums[readp]\n",
    "                readp += 1\n",
    "                writep += 1\n",
    "            elif two==True and nums[readp]==prev:\n",
    "                print(readp,writep,prev)\n",
    "                readp += 1\n",
    "            else:\n",
    "                print(readp,writep,prev)\n",
    "                nums[writep] = nums[readp]\n",
    "                prev=nums[writep]\n",
    "                two = False\n",
    "                readp+=1\n",
    "                writep+=1\n",
    "        return writep\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        if not nums or len(nums)==0:\n",
    "            return 0\n",
    "        length=len(nums)\n",
    "        if length<3:\n",
    "            return length\n",
    "        j=length-1\n",
    "        temp=0\n",
    "        while j>0:\n",
    "            num=0\n",
    "            for i in range(j-1,-1,-1):\n",
    "                if nums[i]==nums[j]:\n",
    "                    num+=1\n",
    "                    temp=i\n",
    "            print(num)\n",
    "\n",
    "            if num>=2:\n",
    "                while num>1:\n",
    "                    nums[temp]=None\n",
    "                    temp+=1\n",
    "                    num-=1\n",
    "\n",
    "                j=temp-1\n",
    "            else:\n",
    "                j-=1\n",
    "        count=0\n",
    "        for i in range(length):\n",
    "            if nums[i]==None:\n",
    "                count+=1\n",
    "        if count>0:\n",
    "           for i in range(count):\n",
    "               nums.remove(None)\n",
    "\n",
    "        return len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 0\n",
    "        for fast in range(len(nums)):\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow += 1\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 0\n",
    "        for fast in range(len(nums)):\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\n",
    "                #对于第三个元素及之后的元素，如果这个元素与它前面的两个元素不相同，说明它出现的次数没有超过两次，放到slow这个位置；\n",
    "                nums[slow] = nums[fast]\n",
    "                slow += 1\n",
    "            # 如果这个元素与它前面的两个元素相同，说明它出现的次数已经超过两次，我们就将它跳过，继续遍历后面的元素。\n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n <=2:\n",
    "            return n\n",
    "        i = 1\n",
    "        for j in range(2,n):\n",
    "            if nums[j]!= nums[i-1]:\n",
    "                nums[i+1] = nums[j]\n",
    "                i+=1\n",
    "        return i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\r\n",
    "    def removeDuplicates(self, nums):\r\n",
    "        slow = 0\r\n",
    "        for fast in range(len(nums)):\r\n",
    "            if slow < 2 or nums[fast] != nums[slow - 2]:\r\n",
    "                nums[slow] = nums[fast]\r\n",
    "                slow += 1\r\n",
    "        return slow\r\n",
    "\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        for i,item in enumerate(collections.Counter(nums)):\n",
    "            while nums.count(item) > 2:\n",
    "                nums.remove(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        nlen=len(nums)\n",
    "        def remove(pre,cur):\n",
    "            if cur==nlen:\n",
    "                return pre+1\n",
    "            else:\n",
    "                if cur==nlen-1:\n",
    "                    nums[pre+1]=nums[cur]\n",
    "                    return remove(pre+1,cur+1)\n",
    "                elif cur==nlen-2:\n",
    "                    nums[pre+1]=nums[cur]\n",
    "                    return remove(pre+1,cur+1)\n",
    "                else:\n",
    "                    if nums[cur]==nums[cur+1] and nums[cur]==nums[cur+2]:\n",
    "                        nums[pre+1]=nums[cur]\n",
    "                        nums[pre+2]=nums[cur]\n",
    "                        pre+=2;cur+=1\n",
    "                        while cur<nlen and nums[pre]==nums[cur]:\n",
    "                            cur+=1\n",
    "                        return remove(pre,cur)\n",
    "                    else:\n",
    "                         nums[pre+1]=nums[cur]\n",
    "                         return remove(pre+1,cur+1)\n",
    "\n",
    "        return remove(-1,0)\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        l=len(nums)\n",
    "        p1,p2=0,1\n",
    "        c,result=1,l\n",
    "        ite=1\n",
    "        while ite<l:\n",
    "            ite+=1\n",
    "            if nums[p2]==nums[p1]:\n",
    "                c=c+1\n",
    "                if c>2:\n",
    "                    for i in range(p2,l-1):\n",
    "                        nums[i]=nums[i+1]\n",
    "                    result-=1\n",
    "                    c=2\n",
    "                else: \n",
    "                    p2+=1\n",
    "            else:\n",
    "                c=1\n",
    "                p1=p2\n",
    "                p2+=1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n <=2: return n\n",
    "\n",
    "        slow, fast = 2, 2\n",
    "        while fast < n:\n",
    "            if nums[fast] != nums[slow-2]:\n",
    "                nums[slow] = nums[fast]\n",
    "                slow +=1\n",
    "            fast +=1\n",
    "        \n",
    "        return slow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDuplicates(self, nums: List[int]) -> int:\n",
    "        slow = 1\n",
    "        length = 1\n",
    "        sums = 1\n",
    "        old_num = nums[0]\n",
    "        for i, num in enumerate(nums[1:]):\n",
    "            if num == old_num and sums == 2:\n",
    "                continue\n",
    "            elif num == old_num and sums == 1:\n",
    "                nums[slow] = num\n",
    "                slow += 1\n",
    "                length += 1\n",
    "                sums += 1\n",
    "            else:\n",
    "                nums[slow] = num\n",
    "                old_num = num\n",
    "                slow += 1\n",
    "                length += 1\n",
    "                sums = 1\n",
    "        return length"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
